package sisicotren.ui.windows;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.Box;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.BoxLayout;

public class WelcomePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private MainWindow owner = null;
	private Boolean smallIcons = false;  //  @jve:decl-index=0:
	private static String imgUrlNameExit = "/img/exit-button-transp";  //  @jve:decl-index=0:
	private static String imgUrlNameSimul = "/img/simulation-button-transp";  //  @jve:decl-index=0:
	private static String imgUrlNameCfg = "/img/configuration-button-transp";  //  @jve:decl-index=0:
	private static String imgUrlNameOptimz = "/img/optimize-button-transp";  //  @jve:decl-index=0:

	/**
	 * This is the default constructor
	 */
	public WelcomePanel(Boolean smallIcons) {
		super();
		this.smallIcons = smallIcons;
		initialize();
	}

	public WelcomePanel(MainWindow parent, Boolean smallIcons) {
		super();
		this.smallIcons = smallIcons;
		initialize();
		this.owner  = parent;
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		Dimension minSize = new Dimension(5, 40);
		Dimension prefSize = new Dimension(5, 40);
		Dimension maxSize = new Dimension(Short.MAX_VALUE, 40);
		this.setBackground(Color.ORANGE);
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		this.setSize(Toolkit.getDefaultToolkit().getScreenSize());
		
		WelcomePanel.imgUrlNameCfg = completeImageName(WelcomePanel.imgUrlNameCfg);
		WelcomePanel.imgUrlNameExit = completeImageName(WelcomePanel.imgUrlNameExit);
		WelcomePanel.imgUrlNameOptimz = completeImageName(WelcomePanel.imgUrlNameOptimz);
		WelcomePanel.imgUrlNameSimul = completeImageName(WelcomePanel.imgUrlNameSimul);
		
		this.add(new Box.Filler(minSize, prefSize, maxSize));
		this.add(getJButton(), null);
		this.add(new Box.Filler(minSize, prefSize, maxSize));
		this.add(getJButton1(), null);
		this.add(new Box.Filler(minSize, prefSize, maxSize));
		this.add(getJButton2(), null);
		this.add(new Box.Filler(minSize, prefSize, maxSize));
		this.add(getJButton3(), null);
	}

	private String completeImageName(String origImgName) {
		return this.smallIcons ? origImgName + "_90x64.png" : origImgName + ".png";
	}
	
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JButton jButton3 = null;

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setName("Simulacion");
			jButton.setContentAreaFilled(false);
			jButton.setFocusPainted(false);
			jButton.setMargin(new Insets(1,1,1,1));
			jButton.setIcon((new javax.swing.ImageIcon(getClass().getResource(WelcomePanel.imgUrlNameSimul))));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					owner.switchToSimulaton();
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setName("Configuracion");
			jButton1.setContentAreaFilled(false);
			jButton1.setFocusPainted(false);
			jButton1.setMargin(new Insets(1,1,1,1));
			jButton1.setIcon((new javax.swing.ImageIcon(getClass().getResource(WelcomePanel.imgUrlNameCfg))));
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ConfigurationDialog dialog = new ConfigurationDialog(owner);
					dialog.open();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setContentAreaFilled(false);
			jButton2.setFocusPainted(false);
			jButton2.setMargin(new Insets(1,1,1,1));
			jButton2.setIcon((new javax.swing.ImageIcon(getClass().getResource(WelcomePanel.imgUrlNameOptimz))));
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					OptimizationResult optRes = new OptimizationResult(owner);
					optRes.show();
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setContentAreaFilled(false);
			jButton3.setFocusPainted(false);
			jButton3.setMargin(new Insets(1,1,1,1));
			jButton3.setIcon((new javax.swing.ImageIcon(getClass().getResource(WelcomePanel.imgUrlNameExit))));
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					// TODO : si hay una simulacion corriendo o
					// cambios en la configuracion no salvados, abrir
					// un dialogo preguntando si se esta seguro
					// de salir.
					System.exit(0);
				}
			});
		}
		return jButton3;
	}
}
